package com.ks.xcamera.view;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ks.basictools.base.BaseActivity;
import com.ks.basictools.manager.ActivityManager;
import com.ks.basictools.utils.ToastUtils;
import com.ks.xcamera.R;
import com.ks.xcamera.adapter.DataAdapter;
import com.ks.xcamera.constant.Constants;
import com.ks.xcamera.databinding.ActivityDataBinding;
import com.ks.xcamera.manager.PreferenceManager;
import com.ks.xcamera.responseBean.ItemModel;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class DataActivity extends BaseActivity {

    private ActivityDataBinding mBinding;
    private DataAdapter mDataAdapter;
    private final List<ItemModel> itemModelList = new ArrayList<>();
    private final AtomicInteger countDownLatch = new AtomicInteger();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusBar(true, true, R.color.colorTrans);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_data);
        mBinding.setActivity(this);
        start();
    }

    private void start() {
        mBinding.mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDataAdapter = new DataAdapter(DataActivity.this);
        mBinding.mRecyclerView.setAdapter(mDataAdapter);

        requestHttp("");

        mBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                requestHttp(s.toString());
            }
        });
    }

    private void requestHttp(String s) {
        itemModelList.clear();
        if (TextUtils.isEmpty(s)) {
            countDownLatch.set(1);
        } else {
            countDownLatch.set(2);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("express_num", s);
        param.put("createtime", "");
        param.put("token", PreferenceManager.getInstance().getToken(this));
        XHttp.post(Constants.URL_EXPRESS_LIST)
                .params(param)
                .onMainThread(true)
                .execute(new SimpleCallBack<List<ItemModel>>() {
                    @Override
                    public void onSuccess(List<ItemModel> beanList) {
                        Log.d("zhxk", "onSuccess: beanList = " + beanList);
                        if (beanList != null) {
                            itemModelList.addAll(beanList);
                        }
                        countDownLatch.addAndGet(-1);
                        if (countDownLatch.get() <= 0) {
                            mDataAdapter.setDataList(itemModelList);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                        ToastUtils.show(DataActivity.this, e.getMessage());
                    }
                });

        if (!TextUtils.isEmpty(s)) {
            param.put("express_num", "");
            param.put("createtime", s);
            param.put("token", PreferenceManager.getInstance().getToken(this));
            XHttp.post(Constants.URL_EXPRESS_LIST)
                    .params(param)
                    .onMainThread(true)
                    .execute(new SimpleCallBack<List<ItemModel>>() {
                        @Override
                        public void onSuccess(List<ItemModel> beanList) {
                            Log.d("zhxk", "onSuccess: beanList = " + beanList);
                            if (beanList != null) {
                                itemModelList.addAll(beanList);
                            }
                            countDownLatch.addAndGet(-1);
                            if (countDownLatch.get() <= 0) {
                                mDataAdapter.setDataList(itemModelList);
                            }
                        }

                        @Override
                        public void onError(ApiException e) {
                            e.printStackTrace();
                            ToastUtils.show(DataActivity.this, e.getMessage());
                        }
                    });
        }
    }

    public void onClick_finish(View ignoreV) {
        ActivityManager.getInstance().finishActivity();
    }
}